package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInstAssureExt;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstAssure;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 销售品实例担保信息构造器
 */
@Component
public class OfferInstAssureExtConstructor extends InstConstructor<KiteOfferInstAssure, OfferInstAssureExt> {

    @Override
    public String getTableName() {
        return TableConsts.OfferInstAssureExt.getTableName();
    }

    @Override
    public InstType getParentInstType() {
        return InstType.PROD_INST;
    }

    @Override
    public OfferInstAssureExt build(OrderRequest orderRequest, String offerInstAssureExtId, Map<String, Object> params) {
        if (Objects.isNull(orderRequest) || StringUtils.isBlank(offerInstAssureExtId)) {
            return null;
        }

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteOfferInstAssure kiteOfferInstAssure = querySceneInstService.getOfferInstAssure(orderRequest.getCustId(),
            offerInstAssureExtId);
        KiteProdInst kiteProdInst = querySceneInstService.getProdInstByOfferInstAssureId(orderRequest.getCustId(),
            offerInstAssureExtId);
        if (isIneffectiveInst(kiteOfferInstAssure) || isIneffectiveInst(kiteProdInst)) {
            return null;
        }

        params = Optional.ofNullable(params).orElse(new HashMap<>());
        params.put(KEY_PARENT_INST_TYPE, kiteProdInst.getInstType());
        params.put(KEY_PARENT_INST_ID, kiteProdInst.getProdInstId());

        kiteOfferInstAssure.setCompInstId(kiteProdInst.getOfferInstId());
        OfferInstAssureExt offerInstAssureExt = convert(orderRequest, kiteOfferInstAssure, params);
        InstContainer.ofRequest(orderRequest).addOfferInstAssureExt(offerInstAssureExt);

        ProdInst prodInst = buildParentInst(orderRequest, kiteProdInst.getProdInstId());
        if (AcceptUtils.isAddInst(offerInstAssureExt) && Objects.nonNull(prodInst)
            && StringUtils.isNotBlank(prodInst.getOwnerCustId())) {
            offerInstAssureExt.setOwnerCustId(prodInst.getOwnerCustId());
        }

        return offerInstAssureExt;
    }

    @Override
    public OfferInstAssureExt newInst(OrderRequest orderRequest, KiteOfferInstAssure kiteOfferInstAssure) {
        if (!ObjectUtils.allNotNull(orderRequest, kiteOfferInstAssure)) {
            return null;
        }

        Date now = new Date();
        OfferInstAssureExt offerInstAssureExt = KiteBeanUtils.transform(kiteOfferInstAssure, OfferInstAssureExt.class,
            KiteInst.ACTION_TYPE);
        offerInstAssureExt.setOfferInstAssureExtId(kiteOfferInstAssure.getOfferInstAssureId());
        offerInstAssureExt.setOwnerCustId(orderRequest.getCustId());
        offerInstAssureExt.setActionType(ActionType.A.name());
        offerInstAssureExt.setStateDate(now);
        offerInstAssureExt.setStaffId(orderRequest.getStaffId());
        offerInstAssureExt.setLanId(orderRequest.getLanId());

        KiteBeanUtils.checkNotNull(offerInstAssureExt);
        return offerInstAssureExt;
    }
}
